class UserBooksController < ApplicationController
  # GET /user_books
  # GET /user_books.json
  def index
    
    if current_user
    
      if params[:collection_id]
        @books = Book.find_all_by_collection_id(params[:collection_id])
      else
      #  @books = Book.find_all_by_user_id(current_user.id)
        @books = Book.joins(:user_book).where("user_id = ?", current_user.id )
      end
   
    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @user_books }
    end
    
    else
      respond_to do |format|
        format.html { redirect_to root_path , :alert => "Please, sign up or log in" }
      end  
    end
    
    
  end

  # GET /user_books/1
  # GET /user_books/1.json
  def show
    @user_book = UserBook.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @user_book }    
    end
  end

  # GET /user_books/new
  # GET /user_books/new.json
  def new
    @user_book = UserBook.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @user_book }
    end
  end

  # GET /user_books/edit
  def edit
    @books = Book.where("collection_id = ?", params[:collection_id])
  end

  # POST /user_books
  # POST /user_books.json
  def create
    @user_book = UserBook.new(params[:user_book])
    @user_book.user_id=current_user.id

    respond_to do |format|
      if @user_book.save
        format.html { redirect_to @user_book, :notice => 'User book was successfully created.' }
        format.json { render :json => @user_book, :status => :created, :location => @user_book }
      else
        format.html { render :action => "new" }
        format.json { render :json => @user_book.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /user_books
  # PUT /user_books.json
  def update
    logger.info("############ PARAM AAAAAAAAAAAA : " + params[:collection_id])
    @books = Book.where("collection_id = ?", params[:collection_id])
    logger.info("############ PARAM CCCCCCC : " + @books.length.to_s)
    @books.each do |book|
      logger.info("############ PARAM BBBBBBBBBBB : " + String.new(book.id.to_s))
      if params[book.id.to_s]["book_id"] == "1"
        if UserBook.where("user_id = ? AND book_id = ?", current_user.id, book.id).empty?
          #create
          @user_book = UserBook.new
          @user_book.user_id = current_user.id
          @user_book.book_id = book.id
          @user_book.save
        end
      else
        if !UserBook.where("user_id = ? AND book_id = ?", current_user.id, book.id).empty?
          #delete
          @user_book = UserBook.where("user_id = ? AND book_id = ?", current_user.id, book.id)
          @user_book.first.destroy
        end
      end
    end
    
    respond_to do |format|
      format.html { redirect_to :action => 'index', :collection_id => params[:collection_id] }
    end
  end

  # DELETE /user_books/1
  # DELETE /user_books/1.json
  def destroy
    @user_book = UserBook.find(params[:id])
    @user_book.destroy

    respond_to do |format|
      format.html { redirect_to user_books_url }
      format.json { head :no_content }
    end
  end
end
